#include "Helpers.h"

#include "Gfx.h"
#include "Input.h"
#include "Core.h"

namespace Helpers
{
	void MoveWithKeys(Obj* obj, float speed, float dt)
	{
		if (IsPressed(KEY_RIGHT))
			obj->Pos.X += speed*dt;
		else if (IsPressed(KEY_LEFT))
			obj->Pos.X -= speed*dt;

		if (IsPressed(KEY_DOWN))
			obj->Pos.Y += speed*dt;
		else if (IsPressed(KEY_UP))
			obj->Pos.Y -= speed*dt;
	}

	void ShootAt(Obj* shooter, const Vec& src_pos, const Vec& dest_pos, Obj* object, float speed)
	{
		object->Pos = src_pos;
		object->Vel = (dest_pos-src_pos).Normalized()*speed;

		shooter->Wrld->AddToWorld(object);
	}

	bool IsOutsideScreen(Obj* obj)
	{
		return (obj->Pos.X > ScreenSize.X || obj->Pos.Y > ScreenSize.Y || obj->Pos.X + obj->Size.X < 0 || obj->Pos.Y + obj->Size.Y < 0);
	}

	bool BounceInsideScreen(Obj* obj)
	{
		bool bounced = false;
		if ((obj->Pos.X > ScreenSize.X && obj->Vel.X > 0) || (obj->Pos.X < 0 && obj->Vel.X < 0))
		{
			obj->Vel.X *= -1;
			bounced = true;
		}
		if ((obj->Pos.Y > ScreenSize.Y && obj->Vel.Y > 0) || (obj->Pos.Y < 0 && obj->Vel.Y < 0))
		{
			obj->Vel.Y *= -1;
			bounced = true;
		}
		return bounced;
	}

	void ScrollOffsetWithMouse(Vec& offset, int mouse_key)
	{
		static Vec grabvec = Vec();
		static bool is_pressed = false;

		Vec pos = GetMousePos();

		if (WasPressed(mouse_key))
		{
			grabvec = pos;
			is_pressed = true;
		} else if (WasReleased(mouse_key))
		{
			is_pressed = false;
		}

		if (is_pressed)
		{
			offset += pos-grabvec;
			printf("%fx%f\n", offset.X, offset.Y);
			grabvec = pos;
		}
	}
}